/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RegisterPanel.java
 *
 * Created on Jul 11, 2011, 4:19:06 PM
 */
package panels;

import conn.EMIS4Connection;
import constants.Constants;
import dao.LoginDao;
import dao.PersonDao;
import dao.PhoneDao;
import dao.PhoneTypeDao;
import dao.SexDao;
import dao.UpdateHistoryDao;
import exec.NotFoundException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import main.MainFrame;
import obj.Login;
import obj.Page;
import obj.Person;
import obj.Phone;
import obj.PhoneType;
import obj.Sex;
import obj.UpdateHistory;
import util.Crypto;
import verification.WantsValidationStatus;

/**
 *
 * @author nknowles
 */
public class RegisterPanel extends javax.swing.JPanel implements WantsValidationStatus {

  private UpdateHistory history;
  private UpdateHistoryDao historyDAO;
  private Login login;
  private LoginDao loginDAO;
  private Person person;
  private PersonDao personDAO;
  private Phone phone;
  private PhoneDao phoneDAO;
  private PhoneTypeDao phoneTypeDAO;
  private SexDao sexDAO;
  private List<Sex> sexObjects;
  private List<PhoneType> phoneTypes;
  private List<Login> loginList;

  /** Creates new form RegisterPanel */
  public RegisterPanel() {
    initComponents();
    EMIS4Connection conn = EMIS4Connection.getInstance();

    history = new UpdateHistory();
    historyDAO = new UpdateHistoryDao();
    login = new Login();
    loginDAO = new LoginDao();
    person = new Person();
    personDAO = new PersonDao();
    phone = new Phone();
    phoneDAO = new PhoneDao();
    phoneTypeDAO = new PhoneTypeDao();
    sexDAO = new SexDao();

    try {
      // populate combo boxes
      conn.connect();
      sexObjects = sexDAO.loadAll(conn.getConnection());
      phoneTypes = phoneTypeDAO.loadAll(conn.getConnection());
    } catch (SQLException ex) {
      Logger.getLogger(RegisterPanel.class.getName()).log(Level.SEVERE, null, ex);
    }
    for (Sex sexItem : sexObjects) {
      sexComboBox.addItem(sexItem.getName());
    }
    for (PhoneType phoneTypeItem : phoneTypes) {
      phoneTypeComboBox.addItem(phoneTypeItem.getTitle());
    }

    usernameTextField.setInputVerifier(new verification.TextFieldVerifier(this, usernameTextField, "Username cannot be blank", Constants.USERNAME_LENGTH_MIN, Constants.USERNAME_LENGTH_MAX));
    passField.setInputVerifier(new verification.PasswordFieldVerifier(this, passField, "Password cannot be blank", Constants.PASSWORD_LENGTH_MIN, Constants.PASSWORD_LENGTH_MAX));
    confirmPassField.setInputVerifier(new verification.PasswordFieldVerifier(this, confirmPassField, "Password cannot be blank", Constants.PASSWORD_LENGTH_MIN, Constants.PASSWORD_LENGTH_MAX));
    firstNameTextField.setInputVerifier(new verification.TextFieldVerifier(this, firstNameTextField, "First name cannont be blank", Constants.FIRST_NAME_LENGTH_MIN, Constants.FIRST_NAME_LENGTH_MAX));
    lastNameTextField.setInputVerifier(new verification.TextFieldVerifier(this, lastNameTextField, "Last name cannot be blank", Constants.LAST_NAME_LENGTH_MIN, Constants.LAST_NAME_LENGTH_MAX));
    emailTextField.setInputVerifier(new verification.EmailFieldVerifier(this, emailTextField, "Email cannot be left blank", Constants.EMAIL_LENGTH_MIN, Constants.EMAIL_LENGTH_MAX));
    phoneTextField.setInputVerifier(new verification.PhoneFieldVerifier(this, phoneTextField, "Phone number cannot be blank", Constants.PHONE_LENGTH));
    dobTextField.setInputVerifier(new verification.DateFieldVerifier(this, dobTextField, "Date of Birth cannont be left blank", Constants.DOB_LENGTH));
    ssnTextField.setInputVerifier(new verification.SsnFieldVerifier(this, ssnTextField, "SSN cannont be left blank", Constants.SSN_LENGTH));
    submitButton.setEnabled(false);
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        warningDialog = new javax.swing.JDialog();
        warningLabel = new javax.swing.JLabel();
        warningButton = new javax.swing.JButton();
        firstNameLabel = new javax.swing.JLabel();
        dobLabel = new javax.swing.JLabel();
        passLabel = new javax.swing.JLabel();
        ssnLabel = new javax.swing.JLabel();
        confirmPassLabel = new javax.swing.JLabel();
        usernameTextField = new javax.swing.JTextField();
        headerLabel = new javax.swing.JLabel();
        usernameLabel = new javax.swing.JLabel();
        firstNameTextField = new javax.swing.JTextField();
        lastNameTextField = new javax.swing.JTextField();
        emailTextField = new javax.swing.JTextField();
        emailLabel = new javax.swing.JLabel();
        lastNameLabel = new javax.swing.JLabel();
        submitButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        confirmPassField = new javax.swing.JPasswordField();
        passField = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        sexComboBox = new javax.swing.JComboBox();
        reWelcomeLabel = new javax.swing.JLabel();
        ssnTextField = new javax.swing.JFormattedTextField();
        dobTextField = new javax.swing.JFormattedTextField();
        phoneTextField = new javax.swing.JFormattedTextField();
        phoneTypeComboBox = new javax.swing.JComboBox();

        warningDialog.setTitle("Warning!");
        warningDialog.setAlwaysOnTop(true);
        warningDialog.setMinimumSize(new java.awt.Dimension(240, 100));
        warningDialog.setModal(true);
        warningDialog.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                warningDialogWindowClosing(evt);
            }
        });
        warningDialog.getContentPane().setLayout(new java.awt.GridBagLayout());

        warningLabel.setText("Message goes here");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE;
        gridBagConstraints.gridheight = java.awt.GridBagConstraints.RELATIVE;
        gridBagConstraints.insets = new java.awt.Insets(15, 0, 10, 0);
        warningDialog.getContentPane().add(warningLabel, gridBagConstraints);

        warningButton.setText("OK");
        warningButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                warningButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 10, 0);
        warningDialog.getContentPane().add(warningButton, gridBagConstraints);

        setBackground(new java.awt.Color(220, 220, 220));
        setOpaque(false);
        setLayout(new java.awt.GridBagLayout());

        firstNameLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        firstNameLabel.setText("First name:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.ipady = 10;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(firstNameLabel, gridBagConstraints);

        dobLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        dobLabel.setText("Date of Birth:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.ipady = 10;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 15, 0, 0);
        add(dobLabel, gridBagConstraints);

        passLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        passLabel.setText("Password:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.ipady = 10;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(passLabel, gridBagConstraints);

        ssnLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        ssnLabel.setText("SSN:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.ipady = 10;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 15, 0, 0);
        add(ssnLabel, gridBagConstraints);

        confirmPassLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        confirmPassLabel.setText("Confirm password:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.ipadx = 10;
        gridBagConstraints.ipady = 10;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(confirmPassLabel, gridBagConstraints);

        usernameTextField.setFont(new java.awt.Font("Tahoma", 0, 12));
        usernameTextField.setName("username"); // NOI18N
        usernameTextField.setNextFocusableComponent(passField);
        usernameTextField.setPreferredSize(new java.awt.Dimension(120, 21));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        add(usernameTextField, gridBagConstraints);

        headerLabel.setFont(new java.awt.Font("Tahoma", 1, 24));
        headerLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        headerLabel.setText("Member Registration Page");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.insets = new java.awt.Insets(15, 0, 10, 0);
        add(headerLabel, gridBagConstraints);

        usernameLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        usernameLabel.setText("Username:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.ipady = 10;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(usernameLabel, gridBagConstraints);

        firstNameTextField.setFont(new java.awt.Font("Tahoma", 0, 12));
        firstNameTextField.setNextFocusableComponent(lastNameTextField);
        firstNameTextField.setPreferredSize(new java.awt.Dimension(120, 21));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        add(firstNameTextField, gridBagConstraints);

        lastNameTextField.setFont(new java.awt.Font("Tahoma", 0, 12));
        lastNameTextField.setNextFocusableComponent(emailTextField);
        lastNameTextField.setPreferredSize(new java.awt.Dimension(120, 21));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        add(lastNameTextField, gridBagConstraints);

        emailTextField.setFont(new java.awt.Font("Tahoma", 0, 12));
        emailTextField.setNextFocusableComponent(phoneTextField);
        emailTextField.setPreferredSize(new java.awt.Dimension(120, 21));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        add(emailTextField, gridBagConstraints);

        emailLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        emailLabel.setText("Email:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.ipady = 10;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 15, 0, 0);
        add(emailLabel, gridBagConstraints);

        lastNameLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        lastNameLabel.setText("Last name:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.ipady = 10;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(lastNameLabel, gridBagConstraints);

        submitButton.setText("Submit");
        submitButton.setToolTipText("Click to submit");
        submitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
        add(submitButton, gridBagConstraints);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel1.setText("yyyy-mm-dd");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        add(jLabel1, gridBagConstraints);

        confirmPassField.setFont(new java.awt.Font("Tahoma", 0, 12));
        confirmPassField.setNextFocusableComponent(firstNameTextField);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        add(confirmPassField, gridBagConstraints);

        passField.setFont(new java.awt.Font("Tahoma", 0, 12));
        passField.setNextFocusableComponent(confirmPassField);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 20;
        add(passField, gridBagConstraints);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setText("Primay Phone:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.ipadx = 10;
        gridBagConstraints.ipady = 10;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 15, 0, 0);
        add(jLabel3, gridBagConstraints);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel4.setText("Sex:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.ipady = 10;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 15, 0, 0);
        add(jLabel4, gridBagConstraints);

        sexComboBox.setNextFocusableComponent(dobTextField);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(sexComboBox, gridBagConstraints);

        reWelcomeLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        reWelcomeLabel.setText("<html><a href>Return to welcome screen</a></html>");
        reWelcomeLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                reWelcomeLabelMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                reWelcomeLabelMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                reWelcomeLabelMouseExited(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
        add(reWelcomeLabel, gridBagConstraints);

        try {
            ssnTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###-##-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        ssnTextField.setNextFocusableComponent(submitButton);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        add(ssnTextField, gridBagConstraints);

        try {
            dobTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("####-##-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        dobTextField.setText("");
        dobTextField.setNextFocusableComponent(ssnTextField);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        add(dobTextField, gridBagConstraints);

        try {
            phoneTextField.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(###) ###-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        phoneTextField.setNextFocusableComponent(phoneTypeComboBox);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        add(phoneTextField, gridBagConstraints);

        phoneTypeComboBox.setNextFocusableComponent(sexComboBox);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        add(phoneTypeComboBox, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

  private void popupWarning(String msg) {
    warningLabel.setText(msg);
    warningDialog.pack();
    warningDialog.setLocationRelativeTo(this);
    warningDialog.setVisible(true);
  }

  private boolean checkAll() {
    boolean valid = true;
    if (usernameTextField.getText().isEmpty()) {
      valid = false;
    }
    if (passField.getPassword().length == 0 || confirmPassField.getPassword().length == 0) {
      valid = false;
    } else if (!(Arrays.equals(confirmPassField.getPassword(), passField.getPassword()))) {
      // Check for matching passwords
      popupWarning("Passwords do not match!");
      passField.requestFocusInWindow();
      confirmPassField.setText("");
      passField.setText("");
      submitButton.setEnabled(false);
      return false;
    }
    if (firstNameTextField.getText().isEmpty()) {
      valid = false;
    } else {
      // Capitalize first letter
      char[] temp = firstNameTextField.getText().toCharArray();
      temp[0] = Character.toUpperCase(temp[0]);
      firstNameTextField.setText(String.valueOf(temp));
    }
    if (lastNameTextField.getText().isEmpty()) {
      valid = false;
    } else {
      // Capitalize first letter
      char[] temp = lastNameTextField.getText().toCharArray();
      temp[0] = Character.toUpperCase(temp[0]);
      lastNameTextField.setText(String.valueOf(temp));
    }
    if (emailTextField.getText().isEmpty()) {
      valid = false;
    }
    if (phoneTextField.getText().replaceAll("[^0-9]", "").isEmpty()) {
      valid = false;
    }
    if (dobTextField.getText().replaceAll("[^0-9]", "").isEmpty()) {
      valid = false;
    }
    if (ssnTextField.getText().replaceAll("[^0-9]", "").isEmpty()) {
      valid = false;
    }
    return valid;
  }

    private void reWelcomeLabelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_reWelcomeLabelMouseClicked
      MainFrame mainFrame = MainFrame.getInstance();
      Page page = new Page();
      page.setTitle("Welcome");
      mainFrame.setPanel(page);
    }//GEN-LAST:event_reWelcomeLabelMouseClicked

    private void warningButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_warningButtonActionPerformed
      warningDialog.setVisible(false);
    }//GEN-LAST:event_warningButtonActionPerformed

    private void warningDialogWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_warningDialogWindowClosing
      this.setVisible(true);
    }//GEN-LAST:event_warningDialogWindowClosing

    private void reWelcomeLabelMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_reWelcomeLabelMouseExited
      reWelcomeLabel.setText("<html><a href>Return to welcome screen</a></html>");
    }//GEN-LAST:event_reWelcomeLabelMouseExited

    private void reWelcomeLabelMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_reWelcomeLabelMouseEntered
      reWelcomeLabel.setText("<html><i><a href>Return to welcome screen</a></i></html>");
    }//GEN-LAST:event_reWelcomeLabelMouseEntered

    private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitButtonActionPerformed
        String pass;

      if (submitButton.isEnabled()) {
        try {
          EMIS4Connection conn = EMIS4Connection.getInstance();
          conn.connect();

          // Store info into respective objects
          person.setFirstName(firstNameTextField.getText()); // TODO parse illegal characters
          person.setLastName(lastNameTextField.getText()); // TODO parse illegal characters
          person.setEmail(emailTextField.getText()); // TODO parse illegal characters
          person.setDOB(util.DateUtils.makeSqlDate(dobTextField.getText()));
          person.setSSN(Integer.valueOf(ssnTextField.getText().replaceAll("[^0-9]", "")));
          person.setSexID(sexComboBox.getSelectedIndex());
          person.setActiveStatus(true);
          person.setRoleID(0);

          // check username avaliability
          login.setUsername(String.valueOf(usernameTextField.getText()));
          loginList = loginDAO.searchMatching(conn.getConnection(), login);
          if (!loginList.isEmpty()) {
            popupWarning("Username already exists, please choose another one.");
            usernameTextField.selectAll();
            usernameTextField.requestFocusInWindow();
            validateFailed();
            return;
          }
          pass = String.valueOf(passField.getPassword());
          
          //  pass = Crypto.getMD5Hash(pass);
          
          login.setPassword(pass);
          login.setLocked(false);

          phone.setNumber(phoneTextField.getText().replaceAll("[^0-9]", ""));
          phone.setActive(true);
          phone.setPhoneTypeID(phoneTypeComboBox.getSelectedIndex());

          // Create new person row in database, this is done first to get the proper auto generated PK (USER ID)
          personDAO.create(conn.getConnection(), person);
          int userID = person.getPersonID();
          history.setAll("Person", userID, "New Entry", "New user registered", "", "New Person");
          historyDAO.create(conn.getConnection(), history);

          // create new login
          login.setPersonID(userID);
          loginDAO.create(conn.getConnection(), login);
          history.setAll("Login", userID, "New Entry", "New user registered", "", "New Login");
          historyDAO.create(conn.getConnection(), history);

          // create new phone
          phone.setPersonID(userID);
          phoneDAO.create(conn.getConnection(), phone);
          history.setAll("Phone", phone.getPhoneID(), "New Entry", "New user registered", "", "New Phone");
          historyDAO.create(conn.getConnection(), history);
        } catch (SQLException ex) {
          Logger.getLogger(RegisterPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NotFoundException ex) {
          Logger.getLogger(RegisterPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

        // goto dashboard
        MainFrame mainFrame = MainFrame.getInstance();
        Page page = new Page();
        page.setTitle("Welcome");
        popupWarning("Thank you, you may now login!");
        mainFrame.setPanel(page);

      } // if not all fields are filled out then display appropriate error message
      else {
        popupWarning("All fields must be filled out!");
      }
    }//GEN-LAST:event_submitButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPasswordField confirmPassField;
    private javax.swing.JLabel confirmPassLabel;
    private javax.swing.JLabel dobLabel;
    private javax.swing.JFormattedTextField dobTextField;
    private javax.swing.JLabel emailLabel;
    private javax.swing.JTextField emailTextField;
    private javax.swing.JLabel firstNameLabel;
    private javax.swing.JTextField firstNameTextField;
    private javax.swing.JLabel headerLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel lastNameLabel;
    private javax.swing.JTextField lastNameTextField;
    private javax.swing.JPasswordField passField;
    private javax.swing.JLabel passLabel;
    private javax.swing.JFormattedTextField phoneTextField;
    private javax.swing.JComboBox phoneTypeComboBox;
    private javax.swing.JLabel reWelcomeLabel;
    private javax.swing.JComboBox sexComboBox;
    private javax.swing.JLabel ssnLabel;
    private javax.swing.JFormattedTextField ssnTextField;
    private javax.swing.JButton submitButton;
    private javax.swing.JLabel usernameLabel;
    private javax.swing.JTextField usernameTextField;
    private javax.swing.JButton warningButton;
    private javax.swing.JDialog warningDialog;
    private javax.swing.JLabel warningLabel;
    // End of variables declaration//GEN-END:variables

  @Override
  public void validateFailed() {
    submitButton.setEnabled(false);
  }

  @Override
  public void validatePassed() {
    if (checkAll()) {
      submitButton.setEnabled(true);
    }
  }
}
